﻿using System;
using System.ComponentModel;
using DoradoFramework.Core;
using Eflong.Utility;

namespace Eflong.Tasks
{
    [Description( "内存定时回收任务" )]
    internal class MemoryRecoveryTask : TaskTemplate<MemoryRecoveryTask>
    {
        public override bool Enabled
        {
            get
            {
                return false;
            }
            set
            {
                _enabled = value;
            }
        }

        /// <summary>
        /// 创建内存定时回收任务
        /// </summary>
        protected override bool CreateTask( )
        {
            if ( TaskHandle == null )
            {
                TaskHandle = GlobalTimer<ITask>.Default.Add( TimeSpan.FromSeconds( 10 ) , new TaskFuncAdapter( ( ) =>
                {
                    try
                    {
                        GC.Collect( );
                    }
                    catch ( Exception ex )
                    {
                        LogWrapper.Logger.Error( ex );
                    }
                } ) , false );//不允许任务重入
            }
            else
            {
                TaskHandle.Start( );
            }
            return true;
        }

        /// <summary>
        /// 销毁内存定时回收任务
        /// </summary>
        /// <returns></returns>
        protected override bool DisposeTask( )
        {
            if ( TaskHandle != null )
            {
                TaskHandle.Dispose( );
                TaskHandle = null;
                return true;
            }
            return false;
        }
    }
}